package calvin.controller.console;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import calvin.utils.RateHelper;

import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;

public class StartController extends Controller {

    @Override
    public Navigation run() throws Exception {
        String password = asString("password");
        if ("mm".equals(password)) {
            if (RateHelper.APP_ID.indexOf("-") == -1) {
                Queue queue = QueueFactory.getQueue("rate-watch-queue");
                queue.add(TaskOptions.Builder.withUrl("/queue/rateWatch"));
            } else {
                Queue queue = QueueFactory.getQueue("rate-fetch-1m-queue");
                queue.add(TaskOptions.Builder.withUrl("/queue/fetchRate5s"));
            }
            requestScope("result", "OK");
        } else {
            requestScope("result", "NG");
        }

        return forward("start.jsp");
    }
}
